package com.earnings.okhttputils.utils.ui.activity.user;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.databinding.ItemHelpBinding;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.adapter.DataBindingBaseadapter;
import com.earnings.okhttputils.utils.adapter.DataBindingViewHolder;
import com.earnings.okhttputils.utils.bean.UserHelpContentData;
import com.earnings.okhttputils.utils.bean.UserHelpTypeData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpArrayCallback;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.ui.activity.common.WebActivity;
import com.earnings.okhttputils.utils.ui.dialog.HelpTypeDialog;

import java.util.List;
import java.util.Map;

import static com.earnings.R.id.phone;


/*
帮助和反馈
 */

public class UserHelpActivity extends GodLeftHandBaseActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private DataBindingBaseadapter baseadapter;
    private TextView name,type;
    private HelpTypeDialog dialog;
    private final int BTN_CHOOSE_NAME=1;
    private final int BTN_CHOOSE_TYPE=2;

    @Override
    protected int onCreateLayoutId() {
        return  R.layout.activity_user_help;
    }

    @Override
    protected void init() {
        setTitle("帮助和反馈");
        //showRightTextView("反馈");
        recyclerView= (RecyclerView) findViewById(R.id.recyclerview);

        name= (TextView) findViewById(R.id.name);
        type= (TextView) findViewById(R.id.type);
        top_right_tv.setOnClickListener( this );
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.phone).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        loadHelpType();
        dialog= new HelpTypeDialog(getContext(), findViewById(R.id.button)) {
            @Override
            public void onClick(String id, String text) {
                name.setText(text);
                loadHelpList( id );
                showBtn(BTN_CHOOSE_NAME);
            }
        };
    }



    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.top_right_tv) {
            skipActivity( UserFeedbcakActivity.class );

        } else if (i == R.id.btn1) {
            showBtn( BTN_CHOOSE_NAME );
            dialog.dismiss();

        } else if (i == R.id.btn2) {
            dialog.show();
            showBtn( BTN_CHOOSE_TYPE );

        }else if (i == R.id.phone) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:400-1052-918" ));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }



    }
/*
改变按钮状态
 */
    public void showBtn(int choose){
        if(choose==BTN_CHOOSE_NAME){
            findViewById(R.id.bg).setBackgroundColor(Color.TRANSPARENT);
            findViewById(R.id.line1).setVisibility(View.VISIBLE);
            findViewById(R.id.line2).setVisibility(View.GONE);
            name.setTextColor(getResources().getColor(R.color.style));
            type.setTextColor(getResources().getColor(R.color.texthigh));
        }
        else  if(choose==BTN_CHOOSE_TYPE) {

                findViewById(R.id.bg).setBackgroundColor(Color.parseColor("#80000000"));
                findViewById(R.id.line2).setVisibility(View.VISIBLE);
                findViewById(R.id.line1).setVisibility(View.GONE);
                type.setTextColor(getResources().getColor(R.color.style));
                name.setTextColor(getResources().getColor(R.color.texthigh));

        }
    }

    public void initData(List<UserHelpContentData> datas){
        if(baseadapter!=null){
        baseadapter.getmDatas().clear();
        baseadapter.notifyDataSetChanged();}
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baseadapter = new DataBindingBaseadapter<UserHelpContentData, ItemHelpBinding>(this, datas, R.layout.item_help) {

            @Override
            public void convert(DataBindingViewHolder holder, ItemHelpBinding binding, UserHelpContentData homeButton) {

                binding.setMsg(homeButton);

            }

            @Override
            public void onItemClick(DataBindingViewHolder holder, UserHelpContentData msgData) {
                bundleData.putString("title",msgData.getTitle());
                bundleData.putString("url",msgData.getUrl());
                skipActivity(WebActivity.class);
            }
        };

        recyclerView.setAdapter(baseadapter);

    }



    public void loadHelpType(){

            Map map = new HttpMap(getContext());


            OkHttpUtils.post().url( HttpUrl.USER_HELP_TYPE_URL ).params( map ).build().execute( new HttpArrayCallback<UserHelpTypeData>(  getContext()) {
                @Override
                public void onSuccess(List<UserHelpTypeData> object, String msg) {
                    dialog.initData( object );
                    if(object.size()>0){
                    loadHelpList("30");
                       //name.setText(object.get( 0 ).getCat_name());
                    }
                }
                @Override
                public void onFail(int code, String msg) {

                    ToastUtils.showToast( getContext(),msg );
                }


            } );


    }
    public void loadHelpList(String id){

        Map map = new HttpMap(getContext());
        map.put( "cat_id", id);
        map.put( "page", "1");
        map.put( "pagesize", "10");

        OkHttpUtils.post().url( HttpUrl.USER_HELP_LIST_URL ).params( map ).build().execute( new HttpArrayCallback<UserHelpContentData>(  getContext()) {
            @Override
            public void onSuccess(List<UserHelpContentData> object, String msg) {
                initData(object);
            }
            @Override
            public void onFail(int code, String msg) {

                ToastUtils.showToast( getContext(),msg );
            }


        } );


    }
}
